package GongNeng;

import GJ.ArrayList;
import Main.Main;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.*;

public class Search extends JFrame {

    JLabel label;
    JLabel label1;
    JPanel panel;
    JButton button;
    JButton button2;
    JTextField textField;
    JComboBox jComboBox;
    JTextArea textArea;
    JScrollPane jScrollPane;
    String bookName;
    String bookNumber;
    String bookAuthor;
    String bookPress;
    boolean flag1,flag2,flag3,flag4;

    public static void main(String args[]){
        Search search = new Search();
        search.setVisible(true);
    }

    public Search(){
        super();
        setTitle("图书查询");
        setBounds(350,100,1200,800);
        getContentPane().setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setBounds(0,0,1200,800);
        panel.setLayout(null);
        getContentPane().add(panel);

        label1 =new JLabel("图书查询");
        label1.setBounds(520,40,300,200);
        label1.setFont(new Font("",Font.BOLD,25));
        panel.add(label1);

        label1 = new JLabel(" 搜索条件：");
        label1.setBounds(840,180,140,100);
        label1.setFont(new Font("",Font.BOLD,22));
        panel.add(label1);

        String[] Books = {"书名","作者","书序","出版社"};
        jComboBox = new JComboBox(Books);
        jComboBox.setEditable(false);
        jComboBox.setMaximumRowCount(4);
        jComboBox.setBounds(950,210,80,40);
        panel.add(jComboBox);

        textField = new JTextField("请输入您要查询的图书信息");
        textField.setBounds(320,210,500,50);
        panel.add(textField);
        textField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if ("".equals("请输入您要查询得信息")){
                    textField.setText("请输入您要查询的图书名");
                }else{
                    textField.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if ("".equals(textField.getText())){
                    textField.setText("请输入您要查询的图书信息！");
                }
            }
        });

        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setEditable(false);
        textArea.setFont(new Font("",Font.BOLD,22));
        jScrollPane = new JScrollPane(textArea);
        jScrollPane.setBounds(250,430,700,285);
        jScrollPane.setVerticalScrollBarPolicy(22);
        panel.add(jScrollPane);

        button2 = new JButton("退出");
        button2.setBounds(660,315,100,50);
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String buttonName = e.getActionCommand();
                if (buttonName.equals("退出")){
                    dispose();
                    new Main().setVisible(true);
                }
            }
        });
        panel.add(button2);


        button = new JButton("查询");
        button.setBounds(410,315,100,50);
        panel.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                    textArea.setText("");
                    String string1 ="书名："+textField.getText();
                    String string2 ="书序："+textField.getText();
                    String string3 ="作者："+textField.getText();
                    String string4 ="出版社："+textField.getText();
                    String song = (String) jComboBox.getSelectedItem();
                                        if (song.equals("书名")){
                                        File file2 = new File("D://Books");
                                        try { BufferedReader bufferedReader=new BufferedReader(new FileReader(file2));
                                        ArrayList arrayList = new ArrayList(100);
                                        String line;String[] song1;
                                        while ((line = bufferedReader.readLine())!= null) {
                                        song1 = line.split(";");
                                        for (int i=0;i<song1.length;i++){
                                        arrayList.addBooks(song1[i]);
                                        }String song4 =arrayList.get(1);
                                        if (song4.equals(string1)) { String[] strings7 = new String[200];
                                        for (int i=0;i<arrayList.size;i++){
                                        strings7[i] = arrayList.get(i);
                                        }
                                        textArea.append(strings7[0]+" "+strings7[1]+" "+strings7[2]+" "+strings7[3] +"\n");
                                        arrayList.clear();
                                        continue;
                                        }else{ arrayList.clear();continue;
                                }
                            }

                        } catch (FileNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }else if(song.equals("书序")){
                        File file2 = new File("D://Books");
                        try {
                            BufferedReader bufferedReader = new BufferedReader(new FileReader(file2));
                            ArrayList arrayList = new ArrayList(100);
                            String line;
                            String[] song1;
                            String zhen;
                            while ((line = bufferedReader.readLine())!= null) {
                                song1 = line.split(";");
                                for (int i=0;i<song1.length;i++){
                                    arrayList.addBooks(song1[i]);
                                }
                                zhen = arrayList.get(0);
                               if (zhen.equals(string2)){
                                   String[] strings8 = new String[200];
                                   for (int i=0;i<arrayList.size;i++){
                                       strings8[i] = arrayList.get(i);
                                   }
                                   textArea.append(strings8[0]+" "+strings8[1]+" "+strings8[2]+" "+strings8[3]+"\n");
                                   arrayList.clear();
                                   continue;
                               }else{
                                   arrayList.clear();
                                   continue;
                               }
                            }
                        } catch (FileNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }else if (song.equals("作者")){
                        File file2 = new File("D://Books");
                        try {
                            BufferedReader bufferedReader = new BufferedReader(new FileReader(file2));
                            ArrayList arrayList = new ArrayList(100);
                            String line;
                            String[] song1;
                            String zhen;
                            while ((line = bufferedReader.readLine())!= null) {
                                song1 = line.split(";");
                                for (int i=0;i<song1.length;i++){
                                    arrayList.addBooks(song1[i]);
                                }
                                zhen = arrayList.get(2);
                                if (zhen.equals(string3)){
                                    String[] strings9 = new String[200];
                                    for (int i=0;i<arrayList.size;i++){
                                        strings9[i]=arrayList.get(i);
                                    }
                                    textArea.append(strings9[0]+" "+strings9[1]+" "+strings9[2]+" "+strings9[3]+"\n");
                                    arrayList.clear();
                                    continue;
                                }else{
                                    arrayList.clear();
                                    continue;
                                }
                            }
                        } catch (FileNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }else{
                    File file2 = new File("D://Books");
                    try {
                        BufferedReader bufferedReader = new BufferedReader(new FileReader(file2));
                        ArrayList arrayList = new ArrayList(100);
                        String line;
                        String[] song1;
                        String zhen;
                        while ((line = bufferedReader.readLine())!= null) {
                            song1 = line.split(";");
                            for (int i=0;i<song1.length;i++){
                                arrayList.addBooks(song1[i]);
                            }
                            zhen = arrayList.get(3);
                            if (zhen.equals(string4)){
                                String[] strings10 = new String[200];
                                for (int i=0;i<arrayList.size;i++){
                                    strings10[i] = arrayList.get(i);
                                }
                                textArea.append(strings10[0]+" "+strings10[1]+" "+strings10[2]+" "+strings10[3]+"\n");
                                arrayList.clear();
                                continue;
                            }else{
                                arrayList.clear();
                                continue;
                            }
                        }
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

    }
}
